package test;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class test {
	 public static void main(String[] args) throws InterruptedException {
	        int peopleNum = 100;//吃饭人数
	        int tableNum = 10;//饭桌数量

	        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
	        
	        
	        CountDownLatch count = new CountDownLatch(10);//计数器

	        //将吃饭人数放入队列（吃饭的人进行排队）
	        for(int i=1;i<=10000;i++){
	            queue.offer("消费者_" + i);
	        }
	        //执行10个线程从队列取出元素（10个桌子开始供饭）
	        System.out.println("-----------------------------------开饭了-----------------------------------");
	        long start = System.currentTimeMillis();
	        //newFixedThreadPool  固定数量的线程池 
	        ExecutorService executorService = Executors.newFixedThreadPool(10);
	        
	        
	        //十个线程相当于10个桌子
	        for(int i=0;i<10;i++) {
	        	//execute没有返回值，而submit有返回值。
	            executorService.submit(new Dinner("00" + (i+1), queue, count));
	        }
	        
	        //计数器等待，直到队列为空（所有人吃完）
	        count.await();
	        long time = System.currentTimeMillis() - start;
	        System.out.println("-----------------------------------所有人已经吃完-----------------------------------");
	        System.out.println("共耗时：" + time);
	        //停止线程池
	        executorService.shutdown();
	    }

	    private static class Dinner implements Runnable{
	        private String name;
	        private ConcurrentLinkedQueue<String> queue;
	        private CountDownLatch count;

	        public Dinner(String name, ConcurrentLinkedQueue<String> queue, CountDownLatch count) {
	            this.name = name;
	            this.queue = queue;
	            this.count = count;
	        }

	        @Override
	        public void run() {
	            //while (queue.size() > 0){
	        	//isEmpty快的多  用这个while循环
	            while (!queue.isEmpty()){
	                //从队列取出一个元素 排队的人少一个
	                System.out.println("【" +queue.poll() + "】----已吃完...， 饭桌编号：" + name);
	            }
	            count.countDown();//计数器-1
	        }
	    }
   
}
